package com.yiwenup.leetcode.daily;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.daily
 * @Author yiwenup
 * @Date 2024-12-11 22:23:56
 * @Description https://leetcode.cn/problems/semi-ordered-permutation/description/?envType=daily-question&envId=2024-12-11
 */
public class No2717 {
    public int semiOrderedPermutation(int[] nums) {
        int idx1 = -1;
        int idxn = -1;
        int n = nums.length;
        for(int i = 0; i < n; i++) {
            if(idx1 != -1 && idxn != -1) {
                break;
            }
            if(nums[i] == 1) {
                idx1 = i;
            }
            if(nums[i] == n) {
                idxn = i;
            }
        }
        if(idx1 < idxn) {
            return idx1 + (n - idxn - 1);
        }
        if(idx1 > idxn) {
            return idx1 + (n - idxn - 1) - 1;
        }
        return 0;
    }
}
